/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import _entity.RoleInfo;
import _entity.UserInfo;
import entity.Roles;
import entity.Users;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;

/**
 *
 * @author HP
 */
@Stateless
public class UsersBean implements UsersBeanRemote {

    @EJB
    private RolesBeanRemote rolesBean;
    @PersistenceUnit(unitName = "_asmPU")
    private EntityManagerFactory emf;

    private EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    @Override
    public List<UserInfo> findAll() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(Users.class));
        List<Users> list = getEntityManager().createQuery(cq).getResultList();
        List<UserInfo> result = new ArrayList<UserInfo>();

        for (Users c : list) {
            UserInfo item = new UserInfo();

            List<RoleInfo> rList = new ArrayList<RoleInfo>();
            for (Roles r : c.getRolesList()) {
                RoleInfo rItem = new RoleInfo();
                rItem.setDescription(r.getDescription());
                rItem.setRoleName(r.getRoleName());
                rItem.setUsersList(result);

                rList.add(rItem);
            }

            item.setEmail(c.getEmail());
            item.setFirstName(c.getFirstName());
            item.setLastName(c.getLastName());
            item.setUserName(c.getUserName());
            item.setUserPass(c.getUserPass());
            item.setRolesList(rList);

            result.add(item);
        }

        return result;
    }

    @Override
    public List<UserInfo> findRange(int[] range) {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(Users.class));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        List<Users> list = q.getResultList();
        List<UserInfo> result = new ArrayList<UserInfo>();

        for (Users c : list) {
            UserInfo item = new UserInfo();

            List<RoleInfo> rList = new ArrayList<RoleInfo>();
            for (Roles r : c.getRolesList()) {
                RoleInfo rItem = new RoleInfo();
                rItem.setDescription(r.getDescription());
                rItem.setRoleName(r.getRoleName());
                rItem.setUsersList(result);

                rList.add(rItem);
            }

            item.setEmail(c.getEmail());
            item.setFirstName(c.getFirstName());
            item.setLastName(c.getLastName());
            item.setUserName(c.getUserName());
            item.setUserPass(c.getUserPass());
            item.setRolesList(rList);

            result.add(item);
        }

        return result;
    }

    @Override
    public int count() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        javax.persistence.criteria.Root<Users> rt = cq.from(Users.class);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }

    @Override
    public UserInfo find(Object id) {
        Users c = getEntityManager().find(Users.class, id);
        if (c != null) {
            UserInfo item = new UserInfo();

            List<RoleInfo> rList = new ArrayList<RoleInfo>();
            for (Roles r : c.getRolesList()) {
                RoleInfo rItem = new RoleInfo();
                rItem.setDescription(r.getDescription());
                rItem.setRoleName(r.getRoleName());

                List<UserInfo> uList = new ArrayList<UserInfo>();
                for (Users u : r.getUsersList()) {
                    UserInfo aU = new UserInfo();
                    aU.setRolesList(rList);
                    aU.setEmail(u.getEmail());
                    aU.setFirstName(u.getFirstName());
                    aU.setLastName(u.getLastName());
                    aU.setUserName(u.getUserName());
                    aU.setUserPass(u.getUserPass());
                }

                rItem.setUsersList(uList);

                rList.add(rItem);
            }

            item.setEmail(c.getEmail());
            item.setFirstName(c.getFirstName());
            item.setLastName(c.getLastName());
            item.setUserName(c.getUserName());
            item.setUserPass(c.getUserPass());
            item.setRolesList(rList);

            return item;
        }
        return null;
    }

    @Override
    public void edit(UserInfo user) {
        Users c = new Users();

        List<Roles> rList = new ArrayList<Roles>();
        for (RoleInfo r : user.getRolesList()) {
            Roles rItem = new Roles();
            rItem.setDescription(r.getDescription());
            rItem.setRoleName(r.getRoleName());
            rItem.setUsersList(rolesBean.find_2(r.getRoleName()).getUsersList());
        }

        c.setEmail(user.getEmail());
        c.setFirstName(user.getFirstName());
        c.setLastName(user.getLastName());
        c.setUserName(user.getUserName());
        c.setUserPass(user.getUserPass());
        c.setRolesList(rList);

        getEntityManager().merge(c);
    }

    @Override
    public void create(UserInfo user) {
        Users c = new Users();

        List<Roles> rList = new ArrayList<Roles>();
        for (RoleInfo r : user.getRolesList()) {
            Roles rItem = new Roles();
            rItem.setDescription(r.getDescription());
            rItem.setRoleName(r.getRoleName());
            rItem.setUsersList(rolesBean.find_2(r.getRoleName()).getUsersList());
        }

        c.setEmail(user.getEmail());
        c.setFirstName(user.getFirstName());
        c.setLastName(user.getLastName());
        c.setUserName(user.getUserName());
        c.setUserPass(user.getUserPass());
        c.setRolesList(rList);

        getEntityManager().persist(c);
    }

    @Override
    public void remove(UserInfo user) {
        Users c = new Users();

        List<Roles> rList = new ArrayList<Roles>();
        for (RoleInfo r : user.getRolesList()) {
            Roles rItem = new Roles();
            rItem.setDescription(r.getDescription());
            rItem.setRoleName(r.getRoleName());
            rItem.setUsersList(rolesBean.find_2(r.getRoleName()).getUsersList());

        }

        c.setEmail(user.getEmail());
        c.setFirstName(user.getFirstName());
        c.setLastName(user.getLastName());
        c.setUserName(user.getUserName());
        c.setUserPass(user.getUserPass());
        c.setRolesList(rList);

        getEntityManager().remove(getEntityManager().merge(c));
    }

    @Override
    public boolean authentication(String userName, String password) {
        Users u = (Users) getEntityManager().createNamedQuery("Users.findByUserName").setParameter("userName", userName).getSingleResult();
        if (u != null) {
            if (u.getUserPass().equals(password)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Users find_2(Object id) {
        return getEntityManager().find(Users.class, id);
    }
}
